<template>
  <el-card>
    <el-table
      :data="menuAdministrationList"
      border
      row-key="id"
      :expand-row-keys="['1']"
    >
      <el-table-column label="名称" prop="name" />
      <el-table-column label="权限值" prop="code" />
      <el-table-column label="跳转权限值" prop="toCode" />
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button
            :icon="Plus"
            type="primary"
            size="small"
            @click="showAddMenu(row)"
          />
          <el-button :icon="Edit" type="primary" size="small" />
          <el-button :icon="Delete" type="danger" size="small" />
        </template>
      </el-table-column>
    </el-table>
    <el-dialog v-model="dialogAddMenu" title="添加菜单">
      <el-form
        label-width="100px"
        ref="addMenuRef"
        :model="addMenuFormData"
        :rules="addMenuFromRules"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="addMenuFormData.name" />
        </el-form-item>
        <el-form-item label="功能值权限" prop="code">
          <el-input v-model="addMenuFormData.code" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="confirmAdd">保存</el-button>
          <el-button @click="dialogAddMenu = false">取消</el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "LPermission",
};
</script>

<script lang="ts" setup>
import { onMounted, reactive, ref } from "vue";
import {
  getAssignPermissionListApi,
  getSavePermissionListApi,
} from "@/api/acl/permission";
import type {
  GetPermissionList,
  GetPermissionItem,
} from "@/api/acl/model/permissionModel";
import { Plus, Edit, Delete } from "@element-plus/icons-vue";
import type { FormInstance, FormRules } from "element-plus";
import { ElMessage } from "element-plus";
const menuAdministrationList = ref<GetPermissionList>([]);
const getMenuAdministrationPage = async () => {
  const result = await getAssignPermissionListApi();
  menuAdministrationList.value = result.children;
};
onMounted(getMenuAdministrationPage);
/********* 添加页面 *************/
const dialogAddMenu = ref(false);
const addMenuRef = ref<FormInstance>();
const addMenuFormData = reactive({
  pid: "",
  name: "",
  code: "",
  type: 1,
  level: 1,
});
const addMenuFromRules = reactive<FormRules>({
  name: [{ required: true, message: "请输入名称", trigger: "blur" }],
  code: [{ required: true, message: "请输入功能权限值", trigger: "blur" }],
});
const showAddMenu = (row: GetPermissionItem) => {
  dialogAddMenu.value = true;
  addMenuFormData.pid = row.id;
  addMenuFormData.name = "";
  addMenuFormData.code = "";
  addMenuFormData.type = row.level + 1 === 4 ? 2 : 1;
  addMenuFormData.level = row.level + 1;
  addMenuRef.value?.validate();
};
// 发送确认请求
const confirmAdd = async () => {
  await getSavePermissionListApi(addMenuFormData);
  dialogAddMenu.value = false;
  ElMessage.success("添加成功");
  getMenuAdministrationPage();
};
</script>

<style scoped></style>
